define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wanlshop/banner/index' + location.search,
                    add_url: 'wanlshop/banner/add',
                    edit_url: 'wanlshop/banner/edit',
                    del_url: 'wanlshop/banner/del',
                    multi_url: 'wanlshop/banner/multi',
                    import_url: 'wanlshop/banner/import',
                    table: 'wanlshop_banner',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'model', title: __('Model'), searchList: {
                                'index': __("Model index"),
                                'optimal': __("Model optimal"),
                                'groupon': __("Model groupon"),
                                'vouchers': __("Model vouchers"),
                                'home_activity': __("Model Home_activity"),

                            },formatter: Table.api.formatter.flag},
                        {field: 'type', title: __('Type') , searchList: {
                                'none': __("Type none"),
                                'goods': __("Type goods"),
                                'shop': __("Type shop"),
                                'vouchers': __("Type vouchers"),

                            },formatter: Table.api.formatter.flag},
                        {field: 'relocation_id', title: __('Relocation_id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'status', title: __('Status') , searchList: {
                                'normal': __("Status normal"),
                                'hidden': __("Status hidden"),
                            },formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "select[name='row[type]']", function () {
                    var type = $("select[name='row[type]']").val();
                    if(type == 'goods'){
                        $('.goods_box').removeClass('hide');
                        $('.shop_box').addClass('hide');
                        $('.vouchers_box').addClass('hide');
                    }
                    else if(type == 'shop'){
                        $('.goods_box').addClass('hide');
                        $('.shop_box').removeClass('hide');
                        $('.vouchers_box').addClass('hide');
                    }
                    else if(type == 'vouchers'){
                        $('.goods_box').addClass('hide');
                        $('.shop_box').addClass('hide');
                        $('.vouchers_box').removeClass('hide');
                    }
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});